package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.common.utils.MyPageHelper;
import com.project.entity.InternInfo;
import com.project.entity.InternLog;
import com.project.entity.StudentInfo;
import com.project.myBeans.R;
import com.project.service.*;
import com.project.service.base.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 23:20
 * @param:
 * @return:
 */
@EnableAutoConfiguration
@RestController
@RequestMapping("/internlog")
@Api(description = "实习日志记录", value = "internlog")
public class InternLogController extends BaseService{
    @Autowired
    private InternlogService internlogService;

    @Autowired
    private InternInfoService internInfoService;

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private StudentInfoService studentInfoService;

    @Autowired
    private StudentService studentService;

    /**
     * TateBrown 2018年5月26日
     *
     * @param internLog
     * @return
     */
    @ApiOperation("模糊搜索分页显示")
    @PostMapping("/findByCondition")
    public R findByCondition(@RequestBody InternLog internLog){
        List<JSONObject> jsonObjectList = new ArrayList<>();
        try {
            internLog.setPageIndex(internLog.getPageIndex() - 1);
            List<InternLog> internInfoList = internlogService.findByCondition(internLog);
            for (InternLog internLog1 : internInfoList) {
                JSONObject jsonObject = internlogService.findDetailOneByInternLog(internLog1);
                jsonObjectList.add(jsonObject);
            }
            return R.ok("查询成功").put("data",jsonObjectList).put("total", MyPageHelper.getTotal(jsonObjectList));
        } catch (Exception e) {
        return R.error("查询失败");
        }
    }

    /**
     * TateBrown 2018年5月26日
     *
     * @param internLog
     * @return
     */
    @ApiOperation("添加实习日志")
    @PostMapping("/addOne")
    public R add(@RequestBody InternLog internLog){
        try {
            //1. 先找到当前学生表的id
            StudentInfo studentInfo = studentInfoService.findOneByUserId(internLog.getComposerid());
            if (studentInfo != null) {
                int studentInfoId = studentInfo.getId();
                //查找正在实习的实习信息表
                InternInfo internInfo = new InternInfo();
                internInfo.setSno(studentInfoId);
                internInfo.setOrgattr(2); //正在实习单位
                internInfo = internInfoService.findOneBySnoAndOragattr(internInfo);
                if (internInfo != null) {
                    internLog.setEmployer(internInfo.getId());
                    internlogService.add(internLog);
                    return R.ok("实习记录添加成功");
                } else {
                    return R.error("学生无正在实习单位");
                }
            } else {
                return R.error("学生信息表记录不存在");
            }
        } catch (Exception e) {
           return R.error();
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id删除实习日志")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id){
        try {
            if (internlogService.delete(id) > 0)
                return R.ok();
            else
                return R.error();
        } catch (Exception e) {
            return R.error();
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param internLog
     * @return
     */
    @ApiOperation("修改实习日志")
    @PostMapping("/modify")
    public R modify(@RequestBody InternLog internLog){
        try {
            if (internlogService.update(internLog) > 0)
                return R.ok();
            else
                return R.error();
        } catch (Exception e) {
            return R.error();
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查找实习日志信息")
    @PostMapping("/findById/{id}")
    public R findById(@PathVariable("id") Integer id){
        try {
            InternLog internLog = internlogService.findById(id);
            if (internLog != null) {
                JSONObject jsonObject = internlogService.findDetailOneByInternLog(internLog);
                return R.ok().put("data",jsonObject);
            } else {
                return R.error("实习记录不存在");
            }
        } catch (Exception e) {
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation("根据internAdvisor查找实习日志详情（即查看该老师指导的学生的实习日志） 根据实习日志条件查找")
    @PostMapping("/findDetailBatchByInternAdvisorCondition")
    public R findDetailBatchByInternAdvisorCondition(@RequestBody JSONObject jsonObject){
        try {
            //解析JSON对象 获得日志/实习指导老师userId/分页大小和页码
            InternLog internLog = new InternLog();
            Integer internAdvisorUserId = -1;
            if (jsonObject.getString("internLog") != null)//实习日志条件
                internLog = JSONObject.parseObject(jsonObject.getString("internLog"), InternLog.class);
            if (jsonObject.getString("internAdvisorUserId") != null)//实习指导老师userId
                internAdvisorUserId = Integer.parseInt(jsonObject.getString("internAdvisorUserId"));
            int pageSize = Integer.parseInt(jsonObject.getString("pageSize"));//分页大小
            int pageIndex = Integer.parseInt(jsonObject.getString("pageIndex")) - 1;//页码

            JSONObject jsonObject1 = internlogService.findDetailBatchByInternAdvisorCondition(internLog, internAdvisorUserId, pageSize,pageIndex);
            return R.ok("查找成功").put("data",jsonObject1.get("data"));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }

}
